package com.c1202m.gui;

import java.awt.Color;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import com.c1202m.dao.DangNhapDAO;
import com.c1202m.util.MyTableFilter;
import com.c1202m.dao.TaiKhoanDAO;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;
import com.c1202m.model.TaiKhoanTableModel;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.swingx.JXTableHeader;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ;

public class BangTaiKhoan extends javax.swing.JPanel {

    TaiKhoanTableModel model = new TaiKhoanTableModel();
    Highlighter simpleStripHL = HighlighterFactory.createSimpleStriping();
    MyTableFilter filterController = null;

    public BangTaiKhoan() {
        initComponents();
        customizeTable();
        setCountLabel();
    }

    private void customizeTable() {
        jxTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxTable.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW,
                null, new Color(255, 102, 0)));
        jxTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (jxTable.getSelectedRow() != -1) {
                    btnEdit.setEnabled(true);
                } else {
                    btnEdit.setEnabled(false);
                }
            }
        });
        TableColumnModel columnModel = jxTable.getColumnModel();
        jxTable.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxTable.getColumn(2).setMaxWidth(60);
        jxTable.getColumn(4).setMaxWidth(70);
        jxTable.getColumn(6).setMaxWidth(65);
        jxTable.getColumn(7).setMaxWidth(65);
        jxTable.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
        filterController = new MyTableFilter(jxTable);
        BindingGroup filterGroup = new BindingGroup();
        filterGroup.addBinding(Bindings.createAutoBinding(READ,
                txtSearch, BeanProperty.create("text"),
                filterController, BeanProperty.create("filterString")));
        filterGroup.bind();
    }

    private static void setCountLabel() {
        lbCountManager.setText("" + jxTable.getRowCount());
    }

    public static void lamMoiBangTK() {
        jxTable.setModel(new TaiKhoanTableModel());
        jxTable.getColumn(2).setMaxWidth(60);
        jxTable.getColumn(4).setMaxWidth(70);
        jxTable.getColumn(6).setMaxWidth(65);
        jxTable.getColumn(7).setMaxWidth(65);
        jxTable.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
        setCountLabel();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jxTable = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        txtSearch = new org.jdesktop.swingx.JXSearchField();
        jLabel3 = new javax.swing.JLabel();
        lbCountManager = new javax.swing.JLabel();
        btnInsert = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        chkHienToanBoTK = new javax.swing.JCheckBox();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setPreferredSize(new java.awt.Dimension(760, 430));

        jxTable.setModel(model);
        jxTable.setToolTipText("Double click để sửa tài khoản");
        jxTable.setColumnControlVisible(true);
        jxTable.setHighlighters(simpleStripHL);
        jxTable.setName("adminTable"); // NOI18N
        jxTable.setShowGrid(false);
        jxTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jxTable);

        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Tìm kiếm theo số tài khoản hoặc chủ tài khoản");
        jLabel1.setPreferredSize(new java.awt.Dimension(220, 15));

        txtSearch.setToolTipText("Nhập từ khóa tìm kiếm");
        txtSearch.setName("bangTaiKhoan"); // NOI18N
        txtSearch.setPreferredSize(new java.awt.Dimension(220, 20));
        txtSearch.setPrompt("Từ khóa tìm kiếm");

        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("Tổng :");

        lbCountManager.setForeground(new java.awt.Color(255, 0, 51));
        lbCountManager.setText("4");

        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        btnInsert.setText("  Thêm");
        btnInsert.setMargin(new java.awt.Insets(2, 10, 2, 10));
        btnInsert.setPreferredSize(new java.awt.Dimension(80, 25));
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/edit.png"))); // NOI18N
        btnEdit.setText("  Sửa");
        btnEdit.setEnabled(false);
        btnEdit.setPreferredSize(new java.awt.Dimension(80, 25));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        btnRefresh.setText("Làm mới");
        btnRefresh.setMargin(new java.awt.Insets(2, 8, 2, 8));
        btnRefresh.setPreferredSize(new java.awt.Dimension(80, 25));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        chkHienToanBoTK.setBackground(new java.awt.Color(255, 255, 255));
        chkHienToanBoTK.setText("Hiện toàn bộ");
        chkHienToanBoTK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkHienToanBoTKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbCountManager))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(btnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(130, 130, 130)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(130, 130, 130)
                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 118, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(chkHienToanBoTK)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkHienToanBoTK))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbCountManager)
                        .addComponent(jLabel3))
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jxTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxTableMouseClicked
        if (evt.getClickCount() >= 2 && jxTable.getSelectedRow() != -1) {
            suaTaiKhoan();
        }
    }//GEN-LAST:event_jxTableMouseClicked

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        lamMoiBangTK();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        suaTaiKhoan();
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        new ThemTaiKhoan(DangNhapDAO.as, false);
    }//GEN-LAST:event_btnInsertActionPerformed

    private void chkHienToanBoTKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkHienToanBoTKActionPerformed
        // TODO add your handling code here:
        if (chkHienToanBoTK.isSelected()) {
            TaiKhoanDAO.hienToanBoTK = true;
            chkHienToanBoTK.setForeground(Color.BLUE);
        } else {
            TaiKhoanDAO.hienToanBoTK = false;
            chkHienToanBoTK.setForeground(Color.BLACK);
        }
        lamMoiBangTK();
    }//GEN-LAST:event_chkHienToanBoTKActionPerformed

    private void suaTaiKhoan() {
        int selected = jxTable.convertRowIndexToModel(jxTable.getSelectedRow());
        String selectedSoTK = (String) jxTable.getModel().getValueAt(selected, 0);
        new SuaTaiKhoan(DangNhapDAO.as, false, selectedSoTK);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JCheckBox chkHienToanBoTK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    public static org.jdesktop.swingx.JXTable jxTable;
    public static javax.swing.JLabel lbCountManager;
    private org.jdesktop.swingx.JXSearchField txtSearch;
    // End of variables declaration//GEN-END:variables
}
